package com.ruoyi.system.service.mongo;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecuritySHA1Utils;
import com.ruoyi.system.domain.Point;
import com.ruoyi.system.domain.mongodb.AllSchool;
import com.ruoyi.system.domain.mongodb.JoinAt;
import com.ruoyi.system.domain.mongodb.Ufollows;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.mongo.AllSchoolDao;
import com.ruoyi.system.mongo.StudentDao;
import com.ruoyi.system.mongo.UfollowsDao;
import com.ruoyi.system.service.IntepralSettingService;
import com.ruoyi.system.service.PointService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author HP
 * @Date 2021/3/23
 * @Description TODO
 */
@Service
public class StudentServiceImpl implements StudentService{
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private IntepralSettingService intepralSettingService;
    @Autowired
    private PointService pointService;
    @Autowired
    private AllSchoolDao allSchoolDao;
    @Autowired
    private UfollowsDao ufollowsDao;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public String saveUser(Users user) throws Exception {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        if (user.getRole()==0){
            List classlists = user.getClasslist();
            if (classlists.size()!=0){
                String klassid = (String) classlists.get(0);
                JoinAt joinAt = new JoinAt();
                String id = UUID.randomUUID().toString().replace("-", "");
                //System.out.println(id);
                joinAt.set_id(new ObjectId(id.substring(0,24)));
                joinAt.setJoinAt(time);
                joinAt.setKlassid(new ObjectId(klassid));
                classlists.clear();
                classlists.add(joinAt);
                user.setClasslist(classlists);
            }else {
                user.setClasslist(null);
            }
        }else{
            user.setClasslist(null);
        }
        user.setStatus(0);
        String schoolname = user.getSchoolName();
        AllSchool schools = allSchoolDao.queryByNames(schoolname);
//        String id = schools.get_id();
        user.setSchool(schools.get_id()!=null?new ObjectId(schools.get_id()):new ObjectId());
        String password = user.getPassword();
        String passwords = SecuritySHA1Utils.shaEncode(password);
        user.setPassword(passwords);
        System.out.println(password);
        String userid = studentDao.saveUser(user);
        Ufollows ufollows=new Ufollows();
        ufollows.setUserid(new ObjectId(userid));
        List list=new ArrayList();
        String yyyyMmDdHhMmSs = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",new Date());
        //ufollows.set_id(new ObjectId(userid));
        ufollows.setCreateAt(yyyyMmDdHhMmSs);
        ufollows.setFanslist(list);
        ufollows.setFolltotal(0);
        ufollows.setFollist(list);
        ufollows.setFanstotal(0);
        ufollowsDao.saveUfollows(ufollows);
        if (userid!=null){
            Point point=new Point();
            if (user.getRole()==0){
                point.setIs_stu(0);
            }else{
                point.setIs_stu(1);
            }
            point.setUser_id(userid);
            point.setPoint(0);
            point.setAccumulated_integral(0);
            point.setIs_delete(0);
            pointService.savePoint(point); //添加学生报名积分账号
            //添加积分
            intepralSettingService.addIntepral(userid,2);
        }
        return userid;
    }
}
